local huimei = fk.CreateSkill{
  name = "hanqing__huimei",
}

Fk:loadTranslationTable{
  ["hanqing__huimei"] = "挥袂",
  [":hanqing__huimei"] = "出牌阶段限一次，你可以与一名手牌数小于你的角色交换装备区里的牌，或与一名装备区牌数小于你的角色交换手牌；"..
  "你可以于发动一项后对另一名角色发动另一项。最后，视为你对区域内总牌数唯一最大的角色使用一张【杀】。",

  ["#hanqing__huimei1"] = "挥袂：你可以与一名手牌数小于你的角色交换装备",
  ["#hanqing__huimei2"] = "挥袂：你可以与一名装备数小于你的角色交换手牌",
  ["hanqing__huimei1"] = "与手牌数小于你的角色交换装备",
  ["hanqing__huimei2"] = "与装备数小于你的角色交换手牌",

  ["$hanqing__huimei1"] = "义不独生，吾宁与赵俱死！",
  ["$hanqing__huimei2"] = "势在垂危，岂可复停时刻？",
}

local function DoHuimei(player, target, choice)
  local room = player.room
  local targets, choice2 = {}, "hanqing__huimei1"
  if choice == "hanqing__huimei1" then
    room:swapAllCards(player, {player, target}, huimei.name, "e")
    targets = table.filter(room:getOtherPlayers(target, false), function (p)
      return #player:getCardIds("e") > #p:getCardIds("e") and not (player:isKongcheng() and p:isKongcheng())
    end)
    choice2 = "hanqing__huimei2"
  else
    room:swapAllCards(player, {player, target}, huimei.name)
    targets = table.filter(room:getOtherPlayers(target, false), function (p)
      return player:getHandcardNum() > p:getHandcardNum() and not (#player:getCardIds("e") == 0 and #p:getCardIds("e") == 0)
    end)
  end
  if player.dead or #targets == 0 then return {} end
  return {targets, choice2}
end

huimei:addEffect("active", {
  anim_type = "control",
  prompt = function (self)
    return "#"..self.interaction.data
  end,
  card_num = 0,
  target_num = 1,
  interaction = UI.ComboBox { choices = {"hanqing__huimei1", "hanqing__huimei2"} },
  can_use = function (self, player)
    return player:usedSkillTimes(huimei.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected == 0 and to_select ~= player then
      if self.interaction.data == "hanqing__huimei1" then
        return player:getHandcardNum() > to_select:getHandcardNum() and
          not (#player:getCardIds("e") == 0 and #to_select:getCardIds("e") == 0)
      else
        return #player:getCardIds("e") > #to_select:getCardIds("e") and
          not (player:isKongcheng() and to_select:isKongcheng())
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local result = DoHuimei(player, target, self.interaction.data)
    while not player.dead do
      if #result == 0 then break end
      target = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = result[1],
        skill_name = huimei.name,
        prompt = "#"..result[2],
        cancelable = true,
      })
      if #target > 0 then
        target = target[1]
        result = DoHuimei(player, target, result[2])
      else
        break
      end
    end
    if player.dead then return end
    local to = table.filter(room.alive_players, function (p)
      return table.every(room:getOtherPlayers(p, false), function (q)
        return #p:getCardIds("hej") > #q:getCardIds("hej")
      end)
    end)
    if #to ~= 1 then return end
    room:useVirtualCard("slash", nil, player, to, huimei.name, true)
  end,
})

return huimei
